package com.gzhryc.shared_device.oem.h5.mng.controllers.member;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.ECouponType;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.business.dao.enums.CouponActivityEnum;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberCoupon;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberCouponEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberCouponService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberCouponSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class MemberCouponController {

    @Route(value = { "/member/coupon/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("couponTypeList", ECouponType.values());
        request.setAttribute("stateList", MemberCouponEnum.State.values());
        return "/member/coupon/list.html";
    }

    @Route(value = "/member/coupon/list.data")
    public ListResult<MemberCoupon> listData(HttpServletRequest request) {
        ListResult<MemberCoupon> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        MemberCouponSearch search = WebUtils.getParameter(request,MemberCouponSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(MemberCouponService.self().countBySearch(search));
        List<MemberCoupon> memberCouponList = MemberCouponService.self().findBySearch(search,pageNo,pageLimit);
        if(memberCouponList != null && memberCouponList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(memberCouponList);
        }
        return result;
    }

    @Route(value = {"/member/coupon/add.html"})
    public String add(HttpServletRequest request) {
        request.setAttribute("useLimitList", CouponActivityEnum.UseLimit.values());
        request.setAttribute("useRangeList", CouponActivityEnum.UseRange.values());
        List<ECouponType> couponTypeList = new ArrayList<>();
        couponTypeList.add(ECouponType.VOUCHERS);
        couponTypeList.add(ECouponType.DISCOUNT_COUPON);
        couponTypeList.add(ECouponType.COUPON_DAY);
        request.setAttribute("couponTypeList",couponTypeList);
        List<EBusinessType> businessTypeList = new ArrayList<>();
        businessTypeList.add(EBusinessType.BXSCN_SHOP);
        businessTypeList.add(EBusinessType.CDB);
        businessTypeList.add(EBusinessType.CDCZ);
        request.setAttribute("businessTypeList",businessTypeList);
        return "/member/coupon/edit.html";
    }

    @Route(value = { "/member/coupon/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        MemberCoupon memberCoupon = WebUtils.getParameter(request,MemberCoupon.class);
        try {
            Double minimumChargeStr = WebUtils.getDouble(request,"minimumChargeStr");
            if(minimumChargeStr != null){
                memberCoupon.setMinimumCharge(NumberTools.changeMoney(minimumChargeStr));
            }
            Double couponMoneyStr = WebUtils.getDouble(request,"couponMoneyStr");
            if(couponMoneyStr != null){
                memberCoupon.setCouponMoney(NumberTools.changeMoney(couponMoneyStr));
            }
            if(MemberCouponService.self().add(memberCoupon)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/member/coupon/info.html" })
    public String info(HttpServletRequest request) {
        String id = WebUtils.getString(request,"id");
        MemberCoupon memberCoupon = MemberCouponService.self().getById(id);
        WebUtils.setAttribute(request,memberCoupon);
        return "/member/coupon/info.html";
    }

    @Route(value = { "/member/coupon/del.do" })
    public AjaxResult del(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String id = WebUtils.getString(request, "id");
        try {
            if(MemberCouponService.self().deleteById(id)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
